{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def gram_schmidt(X):\n",
    "    O = np.zeros(X.shape)\n",
    "\n",
    "    for i in range(X.shape[1]):\n",
    "        # orthogonalization\n",
    "        vector = X[:, i]\n",
    "        space = O[:, :i]\n",
    "        projection = vector @ space\n",
    "        vector = vector - np.sum(projection * space, axis=1)\n",
    "\n",
    "        # normalization\n",
    "        norm = np.sqrt(vector @ vector)\n",
    "        vector /= abs(norm) < 1e-8 and 1 or norm\n",
    "        \n",
    "        O[:, i] = vector\n",
    "\n",
    "    return O"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 6 column vectors in 4D\n",
    "vectors = np.array([\n",
    "    [1, 1, 2, 0, 1, 1],\n",
    "    [0, 0, 0, 1, 2, 1],\n",
    "    [1, 2, 3, 1, 3, 2],\n",
    "    [1, 0, 1, 0, 1, 1]\n",
    "], dtype=float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  3.,   3.,   6.,   1.,   5.,   4.],\n",
       "       [  3.,   5.,   8.,   2.,   7.,   5.],\n",
       "       [  6.,   8.,  14.,   3.,  12.,   9.],\n",
       "       [  1.,   2.,   3.,   2.,   5.,   3.],\n",
       "       [  5.,   7.,  12.,   5.,  15.,  10.],\n",
       "       [  4.,   5.,   9.,   3.,  10.,   7.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check orthogonality\n",
    "vectors.T @ vectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.57735, -0.     , -0.     , -0.30861,  0.     , -0.     ],\n",
       "       [ 0.     ,  0.     ,  0.     ,  0.92582,  0.     ,  0.     ],\n",
       "       [ 0.57735,  0.70711,  0.     ,  0.1543 ,  0.     ,  0.     ],\n",
       "       [ 0.57735, -0.70711, -0.     ,  0.1543 , -0.     , -0.     ]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "orthonormal = gram_schmidt(vectors)\n",
    "orthonormal.round(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1., -0., -0., -0.,  0., -0.],\n",
       "       [-0.,  1.,  0.,  0.,  0.,  0.],\n",
       "       [-0.,  0.,  0.,  0.,  0.,  0.],\n",
       "       [-0.,  0.,  0.,  1.,  0.,  0.],\n",
       "       [ 0.,  0.,  0.,  0.,  0.,  0.],\n",
       "       [-0.,  0.,  0.,  0.,  0.,  0.]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check orthogonality\n",
    "(orthonormal.T @ orthonormal).round(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## QR decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "matrix = np.array([\n",
    "    [1, 1, -1],\n",
    "    [1, 2, 1],\n",
    "    [1, 3, 0]\n",
    "], dtype=float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.57735, -0.70711, -0.40825],\n",
       "       [ 0.57735, -0.     ,  0.8165 ],\n",
       "       [ 0.57735,  0.70711, -0.40825]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q = gram_schmidt(matrix)\n",
    "Q.round(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.73205,  3.4641 ,  0.     ],\n",
       "       [-0.     ,  1.41421,  0.70711],\n",
       "       [ 0.     ,  0.     ,  1.22474]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R = Q.T @ matrix\n",
    "R.round(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  1., -1.],\n",
       "       [ 1.,  2.,  1.],\n",
       "       [ 1.,  3., -0.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(Q @ R).round(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
